package com.sky.service.impl;

import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.operator.Between;
import com.ejlchina.searcher.operator.Equal;
import com.ejlchina.searcher.operator.GreaterThan;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import com.sky.constant.OrderConstant;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.service.WorkspaceService;
import com.sky.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author tzl
 * @Date 2023/8/13 20:42  星期日
 * @ClassName WorkspaceServiceImpl
 * @Description 工作台业务实现类
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private BeanSearcher beanSearcher;


    /**
     * 工作台今日数据查询
     * @return
     */
    @Override
    public Map<String, Object> getBusinessData(String begin, String end) {
        Map<String, Object> result = new HashMap<>();

        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        //查询总订单数
        MapBuilder builder = MapUtils.builder()
                .field(Orders::getOrderTime, begin, end).op(Between.class);
        int totalOrderCount = beanSearcher.searchCount(Orders.class, builder.build()).intValue();

        //营业额
        Map<String, Object> build1 = MapUtils.builder()
                .field(Orders::getOrderTime,begin,end).op(Between.class)
                .field(Orders::getStatus,OrderConstant.COMPLETED).op(Equal.class)
                .build();
        Double turnover = beanSearcher.searchSum(Orders.class, build1, "amount").doubleValue();

        //有效订单数
        builder.field(Orders::getStatus,OrderConstant.COMPLETED).op(Equal.class);
        Integer validOrderCount = beanSearcher.searchCount(Orders.class, builder.build()).intValue();

        double unitPrice = 0.0;

        double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Map<String, Object> build2 = MapUtils.builder()
                .field(User::getCreateTime,begin,end).op(Between.class)
                .build();
        int newUsers = beanSearcher.searchCount(User.class, build2).intValue();

        result.put("turnover",turnover); //营业额
        result.put("validOrderCount",validOrderCount); //有效订单数
        result.put("orderCompletionRate",orderCompletionRate); //订单完成率
        result.put("unitPrice",unitPrice); //平均客单价
        result.put("newUsers",newUsers); //新增用户数
        return result;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public Map<String, Object> getOrderOverView() {
        Map<String, Object> result = new HashMap<>();
        String begin = DateUtils.getDate() + " 00:00:00";

        //待接单
        Map<String, Object> build = MapUtils.builder()
                .field(Orders::getOrderTime,begin).op(GreaterThan.class)
                .field(Orders::getStatus,OrderConstant.TO_BE_CONFIRMED).op(Equal.class)
                .build();
        int waitingOrders = beanSearcher.searchCount(Orders.class, build).intValue();

        //待派送
        Map<String, Object> build1 = MapUtils.builder()
                .field(Orders::getOrderTime,begin).op(GreaterThan.class)
                .field(Orders::getStatus,OrderConstant.CONFIRMED).op(Equal.class)
                .build();
        int deliveredOrders = beanSearcher.searchCount(Orders.class, build1).intValue();

        //已完成
        Map<String, Object> build2 = MapUtils.builder()
                .field(Orders::getOrderTime,begin).op(GreaterThan.class)
                .field(Orders::getStatus,OrderConstant.COMPLETED).op(Equal.class)
                .build();
        int completedOrders = beanSearcher.searchCount(Orders.class, build2).intValue();

        //已取消
        Map<String, Object> build3 = MapUtils.builder()
                .field(Orders::getOrderTime,begin).op(GreaterThan.class)
                .field(Orders::getStatus,OrderConstant.CANCELLED).op(Equal.class)
                .build();
        int cancelledOrders = beanSearcher.searchCount(Orders.class, build3).intValue();

        //已取消
        Map<String, Object> build4 = MapUtils.builder()
                .field(Orders::getOrderTime,begin).op(GreaterThan.class)
                .build();
        int allOrders = beanSearcher.searchCount(Orders.class, build4).intValue();

        result.put("waitingOrders",waitingOrders); //待接单数量
        result.put("deliveredOrders",deliveredOrders); //待派送数量
        result.put("completedOrders",completedOrders); //已完成数量
        result.put("cancelledOrders",cancelledOrders); //已取消数量
        result.put("allOrders",allOrders); //全部订单
        return result;
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public Map<String, Object> getDishOverView() {
        Map<String, Object> result = new HashMap<>();

        //启用
        Map<String, Object> build = MapUtils.builder()
                .field(Dish::getStatus, StatusConstant.ENABLE).op(Equal.class)
                .build();
        int sold = beanSearcher.searchCount(Dish.class, build).intValue();

        //禁用
        Map<String, Object> build1 = MapUtils.builder()
                .field(Dish::getStatus, StatusConstant.DISABLE).op(Equal.class)
                .build();
        int discontinued = beanSearcher.searchCount(Dish.class, build1).intValue();


        result.put("sold",sold); //已启售数量
        result.put("discontinued",discontinued); //已停售数量
        return result;
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public Map<String, Object> getSetmealOverView() {
        Map<String, Object> result = new HashMap<>();

        //启用
        Map<String, Object> build = MapUtils.builder()
                .field(Setmeal::getStatus, StatusConstant.ENABLE).op(Equal.class)
                .build();
        int sold = beanSearcher.searchCount(Setmeal.class, build).intValue();

        //禁用
        Map<String, Object> build1 = MapUtils.builder()
                .field(Setmeal::getStatus, StatusConstant.DISABLE).op(Equal.class)
                .build();
        int discontinued = beanSearcher.searchCount(Setmeal.class, build1).intValue();


        result.put("sold",sold); //已启售数量
        result.put("discontinued",discontinued); //已停售数量
        return result;
    }
}
